package com.webzavod.services;

import com.webzavod.model.Book;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookService {

    @Autowired
    SessionFactory sessionFactory;

    /**
     * @return all books
     */
    @Cacheable(value = "books")
    public List<Book> getAllBooks() {
        return sessionFactory.getCurrentSession()
                .createQuery("from Book")
                .list();
    }
}
